package com.crois.barrier.web.configuration;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.crois.barrier.web.config.BarrierNacosConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.Properties;

/**
 * @author Hou Ze Yu
 * @description   nacos 配置
 * @date 2020/7/13
 */
@EnableConfigurationProperties(BarrierNacosConfig.class)
public class NacosConfigConfiguration {

    /***
     * nacos config
     * @param barrierNacosConfig
     * @return
     * @throws NacosException
     */
    @Bean
    public ConfigService barrierSyncNacosConfigService(final BarrierNacosConfig barrierNacosConfig) throws NacosException {
        Properties properties=new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, barrierNacosConfig.getAddr());
        properties.put(PropertyKeyConst.NAMESPACE, barrierNacosConfig.getNamespace());
        return NacosFactory.createConfigService(properties);
    }

    /**
     * nacos  NamingService
     * @param barrierNacosConfig
     * @return
     * @throws NacosException
     */
    @Bean
    public NamingService doorSyncNacosNameService(final BarrierNacosConfig barrierNacosConfig) throws NacosException {
        Properties properties=new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, barrierNacosConfig.getAddr());
        properties.put(PropertyKeyConst.NAMESPACE, barrierNacosConfig.getNamespace());
        return NacosFactory.createNamingService(properties);
    }

}
